const app = getApp();

// pages/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    info: {},
    list: [],
    b_save: false,
    vide_id_arr: [],
    banner: [],
    scrollHeight: '',
    page: 1,
    isAjax: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    const id = options.id;
    that.infoAction(id, 1);

    let scrollHeight = wx.getSystemInfoSync().windowHeight;
      that.setData({
        scrollHeight: scrollHeight,
      })

    let vide_id_arr = wx.getStorageSync('videoId') || [];
    this.setData({ id: id, vide_id_arr: vide_id_arr });

    if (vide_id_arr.indexOf(id) > -1) {
      this.setData({ b_save: true });
    }

    //统计
    var mta = require('../../utils/mta_analysis.js');
    mta.Page.init();
  },

  infoAction: function(id, page) {
    let that = this;
    if (that.data.isAjax) {
      wx.request({
        url: app.globalData.global_url + '/api/info/' + id,
        data: { page: page },
        type: 'GET',
        success: function (res) {
          if (res.data.code === 200) {
            if (page == 1) {
              that.setData({
                info: res.data.data.info,
                list: res.data.data.list,
                banner: res.data.data.banner,
              })
            } else {
              if (res.data.data.list.length === 0) {
                wx.showToast({
                  title: '暂无更多数据',
                  icon: 'none',
                });

                that.setData({
                  isAjax: false
                });
              } else {
                that.setData({
                  list: that.data.list.concat(res.data.data.list)
                })
              }
            }
          }
        }
      })
    }
  },

  // 保存视频
  saveVideo: function() {
    let that = this;
    let id = that.data.id;
    let vide_id_arr = that.data.vide_id_arr;

    if (!that.data.b_save) {
      // 插入id
      vide_id_arr.push(id);
    } else {
      // 删除id
      vide_id_arr = vide_id_arr.filter(function (value) {
        return value != id
      })
    }

    wx.setStorageSync('videoId', vide_id_arr);
    that.setData({
      b_save: !that.data.b_save,
      vide_id_arr: vide_id_arr
    })
  },
  // 下滑到底部
  pageScrollToBottom: function() {
    let page = this.data.page + 1;
    this.setData({ page: page })
    this.infoAction(this.data.id, page);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    let that = this;
    // if (res.from === 'button') {
    //   console.log("来自页面内转发按钮");
    //   console.log(res.target);
    // }
    return {
      title: that.data.info.title,
      // path: '/pages/info/info?id=' + that.data.id,
      path: '/pages/index/index',
      imageUrl: that.data.info.logo,
      success: (res) => {
        wx.showToast({
          title: '转发成功',
        });

      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  }
})